//
//  SetCityViewController.m
//  XinKaiFa55like
//
//  Created by 55like on 2017/9/23.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "SetCityViewController.h"

@interface SetCityTableViewCell : UITableViewCell<UITextFieldDelegate>

@property (nonatomic,strong)UILabel * titlelab;

@property (nonatomic,strong)UITextField * textField;;

@property (nonatomic,strong)NSIndexPath * indexpath;

@property (nonatomic,copy)void(^block)(NSString *,NSInteger);

@end

@implementation SetCityTableViewCell

-(UILabel *)titlelab{
    
    if (!_titlelab) {
        
        UILabel * lab = [[UILabel alloc]init];
        lab.textColor = rgbTitleDeepGray;
        lab.font = Font(15);
        lab.textAlignment = NSTextAlignmentLeft;
        [self addSubview:lab];
        _titlelab = lab;
        
        
    }
    
    return _titlelab;
}
-(UITextField *)textField{
    
    
    if (!_textField) {
        
        UITextField * baseTextField = [[UITextField alloc]init];
        baseTextField.delegate = self;
        [baseTextField setKeyboardType:UIKeyboardTypeDefault];
        [baseTextField setBorderStyle:UITextBorderStyleNone];
        [baseTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
        [baseTextField setTextColor:rgbTitleDeepGray];
        baseTextField.font=Font(15);
        [baseTextField setSecureTextEntry:NO];
        [baseTextField setReturnKeyType:UIReturnKeyDone];
        baseTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [self addSubview:baseTextField];
        _textField = baseTextField;
        
    }
    
    
    return _textField;
}
#pragma mark -  textfield代理方法
//已经结束编辑
-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    
    if (self.block) {
        self.block(textField.text, self.indexpath.row);
    }
    
}
//return键触发方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self endEditing:YES];
    
    return YES;
}



@end


@interface SetCityViewController ()

@property(nonatomic,strong)UITableView * zhltableView;
@property (nonatomic,strong)NSMutableDictionary * allData;
@property (nonatomic,copy)void(^block)();
@property (nonatomic,copy)void(^dataBlock)(NSMutableDictionary * data);



@end

@implementation SetCityViewController
{
    
    BOOL isreturn;
    
}
-(NSMutableDictionary *)allData{
    
    if (!_allData) {
        
        _allData = [[NSMutableDictionary alloc]init];
        
    }
    
    return _allData;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.allData = [self.userInfo mutableCopy];
    self.block = self.otherInfo[@"block"];
    self.dataBlock = self.otherInfo[@"dataBlock"];
    isreturn = YES;
    self.navrightButton = [self rightButton:@"完成" image:nil sel:@selector(ifSave)];
    [self.navrightButton setTitleColor:rgbpublicColor forState:UIControlStateNormal];
    [self.zhltableView reloadData];
    
    
}
-(void)ifSave{
    
    
    [self.view endEditing:YES];
    
    EMWeakSelf(weakSelf);
    
    [QAlertController showAlertQAlertControllerWithTitle:@"是否保存" message:@"" cancelBtn:@"取消" otherBtnArray:@[@"确定"] showController:self QAlertControllerClickBlock:^(int index) {
       
        
        if (index == 1) {
            
            
            [weakSelf save];
            
        }
        
        
    }];
    
    
    
    
}
-(void)save{
    
    
    [self.allData setValue:[[Utility Share] userId] forKey:@"uid"];
    [self.allData setValue:[[Utility Share] userToken] forKey:@"token"];
    [NetEngine createHttpAction:@"user/address" withCache:NO withParams:self.allData withMask:SVProgressHUDMaskTypeNil onCompletion:^(id resData, BOOL isCache) {
        [SVProgressHUD showImage:nil status:[resData valueForJSONStrKey:@"info"]];
        if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
            
            isreturn = YES;
            
            if (self.block) {
                self.block();
            }
            if (self.dataBlock) {
                self.dataBlock(self.allData);
            }
            [self backButtonClicked:nil];
            
            
        }else{
            
            
            
            
            
            
        }
        
    } onError:^(NSError *error) {
        
    }];
    
    
    
    
}
static NSString * SetCityTableViewCellID  = @"SetCityTableViewCellID";

-(UITableView *)zhltableView{
    
    if (!_zhltableView) {
        
        UITableView * zhltableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) style:UITableViewStyleGrouped];
        zhltableView.delegate = self;
        zhltableView.dataSource = self;
        zhltableView.backgroundColor = rgb(242, 242, 242);
        zhltableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        zhltableView.showsVerticalScrollIndicator = NO;
        zhltableView.showsHorizontalScrollIndicator = NO;
        zhltableView.tableHeaderView.frameHeight = 0;
        [self.view addSubview:zhltableView];
        [zhltableView registerClass:[SetCityTableViewCell class] forCellReuseIdentifier:SetCityTableViewCellID];
        _zhltableView = zhltableView;
        
    }
  
    
    return _zhltableView;
}
#pragma mark -  tableview代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    return 1;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    
    return self.titleArray.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return 44;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section    {
    
    
    
    return CGFLOAT_MIN;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    
    return [UIView new];
}
#pragma mark  cell处理
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SetCityTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SetCityTableViewCellID forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSString * title = self.titleArray[indexPath.row];
    cell.indexpath = indexPath;
    cell.titlelab.text = title;
    cell.titlelab.frame = CGRectMake(10, 0, 75, cell.frameHeight);
    cell.textField.frame = CGRectMake(XW(cell.titlelab) + 10, 0,kScreenWidth - XW(cell.titlelab) - 20, cell.frameHeight);
    cell.textField.placeholder = self.placeholderArray[indexPath.row];
    cell.textField.text = self.textArray[indexPath.row];
    cell.textField.userInteractionEnabled = YES;
    
    if ([title isEqualToString:@"选择地区"]) {
        
        cell.textField.userInteractionEnabled = NO;
        
    }
    EMWeakSelf(weakSelf);
    cell.block = ^(NSString * text, NSInteger num) {
        
        isreturn = NO;
        
        switch (num) {
            case 0:
            {
                [weakSelf.allData setValue:text forKey:@"addname"];
            }
                break;
            case 1:
            {
                [weakSelf.allData setValue:text forKey:@"addtel"];
   
            }
                break;
            case 2:
            {
            }
                break;
            case 3:
            {
                [weakSelf.allData setValue:text forKey:@"address"];
            }
                break;
            case 4:
            {
                [weakSelf.allData setValue:text forKey:@"postal"];
            }
                break;
            default:
                break;
        }
        
        
        
    };
    
    
    
    return cell;
    
}
#pragma mark  选中cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    EMWeakSelf(weakSelf);
    NSLog(@"%@",indexPath);
    if ([self.titleArray[indexPath.row] isEqualToString:@"选择地区"]) {
        
        [self.view endEditing:YES];
        
        
        [QPickerView showSeachQPickerViewWithTitle:@"请选择地址" Data:nil scrollToProvince:[self.allData valueForJSONStrKey:@"province"] city:[self.allData valueForJSONStrKey:@"city"] town:[self.allData valueForJSONStrKey:@"area"] dataBlock:^(NSMutableArray *titleArray, NSMutableArray *indexArray, NSMutableArray *indexDataArray) {
           
            isreturn = NO;
            [weakSelf.allData setValue:@"" forKey:@"province"];
            [weakSelf.allData setValue:@"" forKey:@"city"];
            [weakSelf.allData setValue:@"" forKey:@"area"];
            [weakSelf.allData setValue:@"" forKey:@"provinceid"];
            [weakSelf.allData setValue:@"" forKey:@"cityid"];
            [weakSelf.allData setValue:@"" forKey:@"areaid"];
            for (int i = 0; i < titleArray.count; i ++) {
                
                NSString * str = titleArray[i];
                NSDictionary * dic = indexDataArray[i];
                switch (i) {
                    case 0:
                    {
                        [weakSelf.allData setValue:str forKey:@"province"];
                        [weakSelf.allData setValue:[dic valueForJSONStrKey:@"areaid"] forKey:@"provinceid"];
                    }
                        break;
                    case 1:
                    {
                        [weakSelf.allData setValue:str forKey:@"city"];
                        [weakSelf.allData setValue:[dic valueForJSONStrKey:@"areaid"] forKey:@"cityid"];
                    }
                        break;
                    case 2:
                    {
                        [weakSelf.allData setValue:str forKey:@"area"];
                        [weakSelf.allData setValue:[dic valueForJSONStrKey:@"areaid"] forKey:@"areaid"];
                    }
                        break;
                    default:
                        break;
                }
                
                
            }
            
            
            
            
            [weakSelf.zhltableView reloadData];
            
        }];
        
        
    }
    
}
-(void)backButtonClicked:(id)sender{
    
    
    [self.view endEditing:YES];
    
    if (isreturn == NO) {
        
        [QAlertController showAlertQAlertControllerWithTitle:@"信息已修改" message:@"" cancelBtn:@"退出" otherBtnArray:@[@"保存退出"] showController:self QAlertControllerClickBlock:^(int index) {
           
            if (index == 0) {
                
                
                [super backButtonClicked:sender];
                
            }else{
                
                [self save];
  
            }
            
            
        }];
        
        
        return;
    }
    
    [super backButtonClicked:sender];
    
    
    
}
-(NSArray *)titleArray{
    
    
   
     return @[@"联系人",@"手机号码",@"选择地区",@"详细地址",@"邮政编码"];
}
-(NSArray *)textArray{
    
    return @[ [self.allData valueForJSONStrKey:@"addname"],
              [self.allData valueForJSONStrKey:@"addtel"],
              [NSString stringWithFormat:@"%@ %@ %@",
               [self.allData valueForJSONStrKey:@"province"],
               [self.allData valueForJSONStrKey:@"city"],
               [self.allData valueForJSONStrKey:@"area"]],
              [self.allData valueForJSONStrKey:@"address"],
              [self.allData valueForJSONStrKey:@"postal"]];
   
}
-(NSArray *)placeholderArray{
    
    
    return @[@"请输入联系人",@"请输入手机号码",@"请选择地区",@"请输入详细地址",@"请输入邮政编码"];
}

@end
